<?php
/**
 * Created by PhpStorm.
 * User: 72925
 * Date: 2018/11/27
 * Time: 15:40
 * order: 个人中心
 */

namespace app\index\controller;

use think\Controller;
use think\Validate;
use app\index\model\Contact;
use app\index\model\Advert;
use app\index\model\User;
use app\index\model\Fbtask;
use app\index\model\Task;
use app\index\model\Account;
use think\Cache;


class Personal extends Base
{	

	/*QR_code*/
 	public function index()
 	{                             
 		/*判断当前用户*/
 			/*扫描二维码的前提是必须小程序必须上线*/
 				$id= input('id');
                $status = $this->status;
                if($status == "error"){
                        $dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
                        return json_encode($dat);
                }
                else
                {
                        $user = User::get(['id'=>$id]);
                        $res = $user->toArray();
                        $codepic = Contact::where('id',1)->value('codepic'); 						
                        $arr = array('invite' => $res['invite'], 'codepic' => $codepic);
                        $dat = ['code' => 200, 'msg' => '返回数据成功', $arr];
                        return json_encode($dat, TRUE);
                }

 		
 	} 





	//获取当前用户的总收入，账户余额， 已经体现的余额
	public function showcount()
	{
		$userID= input('id');
        $status = $this->status;
		if($status == "error"){
			$dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
	        return json_encode($dat);
		}else{
			//查询当前用户接到的所有单子的总金额 
// 
			$task = new Task;
			$fbid = $task->where('uid',$userID)->where('status',2)->column('fbid');
			$money = [];
			foreach ($fbid as $key => $value) {
				$res = db('fbtask')->where('id',$value)->sum('money');	
				$money[] = $res;
			}
			$rnumber = User::where(['id'=>$userID])->value('rnumber');//账户余额
			$allmoney = array_sum($money);//总收入						
			$balance = User::where(['id'=>$userID])->value('balance');//账户余额			
			$withdraw = Account::where(['rid'=>$userID])->sum('price');//已提现金额
			$data = array('allmoney'=>$allmoney,'balance'=>$balance,'tixian'=>$withdraw,'rnumber'=>$rnumber);
			if($allmoney !== "" || $balance !== "" || $withdraw !== "" ){
				$dat = array('code'=>200,'msg'=>'返回数据成功','data'=>$data);
				echo json_encode($dat,JSON_FORCE_OBJECT);
			}else{
				$dat = array('code'=>200,'msg'=>'暂无数据',$data);
				echo json_encode($dat,JSON_FORCE_OBJECT);
			}
			
		


		}
	}




	 //我的团队
	 public function team()
	 {

	 	//点开我的团队要显示出普通会员和高级会员的人数
	 		$id = input('id');
	 		//搜索出ivt 中当前用户id的数量
	 		//ivt =1 and grade =1 || grade =2;
	 		//普通用户
	 		$status = $this->status;
			if($status == "error"){
				$dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
		        return json_encode($dat);
			}elseif($status == "err"){
				$dat = ['code' => 404, 'msg' => '请传入参数'];
		        return json_encode($dat);
					
			}else{
				$num1 = User::where('ivt',$id)->where('grade',1)->count();
		 		$num2 = User::where('ivt',$id)->where('grade',2)->count();
		 		$data = array('pt'=>$num1,'gj'=>$num2);
		 		$dat = array('code'=>200,'msg'=>'返回数据成功',$data);
				echo json_encode($dat,JSON_FORCE_OBJECT);
			}

	 		 	
	 }


	 //我的团队详情
	 public function teamdetail()
	 {
	 	//点开普通会员或者高级会员要实现出每个会员的基本信息
	 	$id = input('id');
	 	$sign = input('sign');
		$status = $this->status;
		if($status == "error"){
			$dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
		        return json_encode($dat);
		}elseif($status == "err"){
			$dat = ['code' => 404, 'msg' => '请传入参数'];
		    return json_encode($dat);
					
		}else{ 	
				$num1 = User::where('ivt',$id)->where('grade',$sign)->field(['rnumber','created_at','grade'])->select();
		 		$res = $num1->toArray();
		 		if(!$res){
					$dat = array('code'=>400,'msg'=>'返回数据为空');
					echo json_encode($dat,JSON_FORCE_OBJECT);
		 		}else{
		 			$dat = array('code'=>200,'msg'=>'返回数据成功',$res);
					echo json_encode($dat,JSON_FORCE_OBJECT);
		 		}
	 }

}



	 public function editpass()
	 {
		 	/*接受信息并验证密码规则*/
		 	$oldpassword =  trim(input('oldpassword'));//旧密码
	        $newpassword =  trim(input('newpassword')); //新密码
	        $repassword =   trim(input('repassword')); //重复密码
	        $id =   trim(input('id'));  //用户身份
	        $status = $this->status;
			if($status == "error"){
				$dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
			        return json_encode($dat);
			}elseif($status == "err"){
				$dat = ['code' => 404, 'msg' => '请传入参数'];
			    return json_encode($dat);
						
			}else{

	        $rule = [                   
                    'newpassword' => 'alphaNum|length:6,18',
                    'repassword'=> 'alphaNum|length:6,18',
                ];

                $msg = [

                    'newpassword' => '密码为6到18位大小写字母和数字组合',
                    'repassword' => '密码为6到18位大小写字母和数字组合',
                ];
        
                $datas = [
                    'newpassword' => $newpassword,
                    'repassword'=> $repassword,
                ];

            $validate = new Validate($rule,$msg);
            $result = $validate->check($datas);    
	        	$testpassword = User::where('id',$id)->value('password');
	            if( MD5($oldpassword) === $testpassword )
	            {	
	            	if($result)
	            	{	
		            	if($newpassword == $repassword)
		            	{
		            		$user = new User;
		            		$update=$user->where('id',$id)->update(['password'=>md5($newpassword)]);
		            		$dat = array('code'=>200,'msg'=>'密码修改成功');
		                    echo json_encode($dat,JSON_FORCE_OBJECT);
		            	}
		            	else
		            	{
		            		 $dat = array('code'=>402,'msg'=>'新密码和重复密码密码不一致');
		                	 echo json_encode($dat,JSON_FORCE_OBJECT);
		            	}


			        }
			        else
			        {
			        	 $dat = array('code'=>403,'msg'=>'密码不符合规则');
			             echo json_encode($dat,JSON_FORCE_OBJECT);
			        }
	            }
	            else
	            {

	            	$dat = array('code'=>405,'msg'=>'原密码不正确');
			        echo json_encode($dat,JSON_FORCE_OBJECT);
	            }
	        }
      }       


    public function forget(){
    	$mobile = trim(input('mobile'));
        $verify = trim(input('verify'));
        $password = trim(input('password'));
        $repassword = trim(input('repassword'));
 		$is_code = Cache::get('param');//获取缓存中的验证码
        $rule  = [  'password' => 'alphaNum|length:6,18',  'repassword'=> 'alphaNum|length:6,18',   ];
		$msg   = [  'password' => '密码为6到18位大小写字母和数字组合',  'repassword' => '密码为6到18位大小写字母和数字组合',   ];
	    $datas = [  'password' =>  $password,  'repassword'=> $repassword,   ];
		$validate = new Validate($rule,$msg);
        $result = $validate->check($datas);
        //数据判断
        $isphone = user::where('mobile',$mobile)->find();
        if($isphone){
        	if($verify == $is_code){
        		//密码判断
        		if($result && $password == $repassword){
        				$user = new User;
					$time = date('Y-m-d:H:i:s');//当前时间
		            	$update=$user->where('mobile',$mobile)->update(['password'=>md5($password),'updated_at'=>$time]);
		            	$dat = array('code'=>200,'msg'=>'密码重置成功');
		                echo json_encode($dat,JSON_FORCE_OBJECT);
        		}else{
        			$dat = ['code' => 402, 'msg' => '密码和重复密码不一致'];
                    return json_encode($dat);
        		}
        	}else{
        		$dat = ['code' => 400, 'msg' => '验证码错误'];
                return json_encode($dat);
        	}
        }else{
        	$dat = ['code' => 403, 'msg' => '此手机号码不存在,请注册'];
            return json_encode($dat);
        }
    }




	 public function outlogin()
	 {
	 	$id = input('id');
	 	$status = $this->status;
			if($status == "error"){
				$dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
			        return json_encode($dat);
			}elseif($status == "err"){
				$dat = ['code' => 404, 'msg' => '请传入参数'];
			    return json_encode($dat);						
			}else{				
				 $outlogin= User::where('id',$id)->update(['updated_at'=> '']);
				 if($outlogin){
				 	$dat = array('code'=>200,'msg'=>'退出成功');
			        echo json_encode($dat,JSON_FORCE_OBJECT);
				 }else{
				 	$dat = array('code'=>200,'msg'=>'退出成功');
			        echo json_encode($dat,JSON_FORCE_OBJECT);
				 }
			}

	 }






}
